<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFullScreen?'exit-fullscreen': 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },
  mounted() {
    // 解决esc退出全屏，图标不切换的问题，利用监听改变isFullScreen的值，便于图标判断切换
    // window.addEventListener('fullscreenchange', e => {
    //   const isFull = document.fullscreenElement
    //   // 判断是否为null 表示为非全屏
    //   if (!isFull) {
    //     this.isFullScreen = false
    //   }
    // })

    // 监控事件的触发 利用插件实现图标的切换  让isFullScreen 的状态始终与是否全屏的状态保持一致
    screenfull.on('change', () => {
      this.isFullScreen = screenfull.isFullscreen
    })
  },
  methods: {
    toggleScreen() {
      // if (this.isFullScreen) {
      //   document.exitFullscreen()
      // } else {
      //   document.documentElement.requestFullscreen()
      // }
      // this.isFullScreen = !this.isFullScreen

      // 利用插件实现全屏的切换
      if (screenfull.isEnabled) {
        screenfull.toggle()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
